﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Business;

namespace UFIDA.U9.CusDev.AITPlugInBE
{
    public class FileUtils
    {
        private static Dictionary<string, object> _cache = new Dictionary<string, object>();
        public static void Write(String filepath,String content)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + filepath;
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.Write(content);
                }
            }
            catch (Exception e)
            {
                LogUtils.logger.Error($"写入文件[{filepath}]失败!错误消息:{e}");
            }
        }

        /// <summary>
        /// 读取portal目录下的文件
        /// </summary>
        /// <param name="filepath">portal下的相对路径</param>
        /// <returns></returns>
        public static string Read(String filepath)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory+filepath;
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader(path))
                {
                    string line;
                    // 从文件读取并显示行，直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        sb.AppendLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                LogUtils.logger.Error($"读取文件[{filepath}]失败!错误消息:{e}");
                return null;
            }
            return sb.ToString();
        }

        public static JObject GetEntityConfig(BusinessEntity entity)
        {
            string key = "AITEntityInfo";
            JObject jconfig;
            JObject json;
            if (_cache.ContainsKey(key))
            {
                jconfig = (JObject)_cache[key];
            }
            else
            {
                String jsonstr = FileUtils.Read("\\bin\\AITEntityInfo.json");
                if (jsonstr == null || jsonstr.Length == 0) return null;
                jconfig = (JObject)JsonConvert.DeserializeObject(jsonstr);
                _cache.Add(key, jconfig);
            }
            json = (JObject)jconfig[entity.GetType().FullName];
            if (json == null) return null;
            return json;
        }
        /// <summary>
        /// 获取探针配置
        /// 配置文件 \\bin\\AITCall.json
        /// </summary>
        /// <returns></returns>
        public static JObject GetAITConfig()
        {
            string key = "GetAITConfig";
            string path = "\\bin\\AITCall.json";
            JObject jconfig;

            if (_cache.ContainsKey(key))
            {
                jconfig= (JObject)_cache[key];
            }
            else
            {
                String jsonstr = FileUtils.Read(path);
                if (jsonstr == null || jsonstr.Length == 0)
                    return null;
                jconfig = (JObject)JsonConvert.DeserializeObject(jsonstr);
                _cache.Add(key, jconfig);
            }
            
            return jconfig;
        }


    }
}
